package cn.hbwe.controller;


import cn.hbwe.pojo.dto.ChartDTOy;
import cn.hbwe.pojo.entity.Chart;
import cn.hbwe.pojo.vo.BookingVo;
import cn.hbwe.pojo.vo.ChartVo;
import cn.hbwe.service.IBookingService;
import cn.hbwe.service.IChartService;
import cn.hbwe.service.impl.BookingServiceImpl;
import cn.hbwe.util.PageVO;
import cn.hbwe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import cn.hbwe.pojo.dto.ChartDTO;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/chart")
@Api(tags = "病历类操作")
@CrossOrigin
public class ChartController {
    @Autowired
    private IChartService iChartService;

    @Autowired
    private IBookingService bookingService;
    @PostMapping("/insertChart")
    public Result insertChart(@RequestBody @Valid cn.hbwe.pojo.dto.ChartDTO chartDTO, BindingResult bindingResult){
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            return Result.error(error.getDefaultMessage());
        }
        return  iChartService.insertChart(chartDTO);

    }

    private class ChartDTO {
    }





    @ApiOperation("用户端查询病历")
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result<PageVO<ChartVo>> getPageById(@PathVariable Long currentPage, @PathVariable Long pageSize, Integer id) {
        PageVO<ChartVo> page = iChartService.getPageById(currentPage, pageSize, id);
        return Result.success(page);
    }

    @ApiOperation("查询全部病历")
    @GetMapping("/page1/{currentPage}/{pageSize}")
    public Result<PageVO<ChartVo>> getPageById1(@PathVariable Long currentPage, @PathVariable Long pageSize,String name) {
        PageVO<ChartVo> page = iChartService.getPage(currentPage, pageSize,name);
        return Result.success(page);
    }

    @ApiOperation("病历信息修改")
    @PutMapping("/update")
    public Result<String> modify(@RequestBody Chart chart) {
        iChartService.updateById(chart);
        return Result.success("病历信息修改成功");
    }

    //------------------y------------------
@ApiOperation("医生端查询病历")
@GetMapping("/getPageById/{currentPage}/{pageSize}")
public Result<PageVO<ChartVo>> getPageBypId(@PathVariable Long currentPage, @PathVariable Long pageSize, Integer id) {
    PageVO<ChartVo> page = iChartService.getPageById(currentPage, pageSize, id);
    return Result.success(page);
}


    @PostMapping("/dinsertChart")
    public Result insertChart(@RequestBody @Valid ChartDTOy chartDTO, BindingResult bindingResult){
        System.out.println("++++++++");
        System.out.println(chartDTO.getBookingId()+"getPatientId"+chartDTO.getPatientId()+"DoctorId"+chartDTO.getDoctorId()
                +"ChartRecord"+chartDTO.getChartRecord()+"  tDoctorNam"+chartDTO.getDoctorName()+"  getPatientname"+chartDTO.getPatientName()

        );
        System.out.println("+++++++++");
        List<ObjectError> errors = bindingResult.getAllErrors();
        for (ObjectError error : errors) {
            System.out.println(error.getDefaultMessage());
            return Result.error(error.getDefaultMessage());

        }
        return  iChartService.insertCharty(chartDTO);

    }
}
